<template>
  <div class="login">
    <div class="container">
      <div class="wrapper-login">
        <div class="img">
          <img src="../../assets/logo.png" alt />
        </div>
        <el-form class="login-form" ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item prop="username">
            <el-input v-model="form.username" prefix-icon="iconfont icon-geren"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="form.password" prefix-icon="iconfont icon-mima"></el-input>
          </el-form-item>
          <el-form-item class="form-btn">
            <el-button type="primary" @click="submitForm()">登录</el-button>
            <el-button type="info" @click="resetForm()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'change' }]
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs.form.validate(async val => {
        if (!val) {
          return this.$message({
            message: '用户名或密码有误',
            type: 'warning'
          })
        }
        const { data: res } = await this.$axios.post('login', this.form)
        if (res.meta.status === 200) {
          this.$message.success('登录成功')
          window.sessionStorage.setItem('token', res.data.token)
          this.$router.push('/home')
        }
      })
    },
    resetForm() {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang='less' scoped>
.login {
  background: #2b4b6b;
  height: 100%;
  .container {
    position: relative;
    height: 100%;
    .wrapper-login {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      width: 450px;
      height: 300px;
      background: #fff;
      border-radius: 5px;
      .img {
        width: 120px;
        height: 120px;
        border-radius: 50%;
        overflow: hidden;
        border: 1px solid #ccc;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        top: -50px;
        background: #fff;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        img {
          width: 100%;
          height: 100%;
          border-radius: 50%;
        }
      }
      .login-form {
        padding: 25px;
        margin-top: 80px;
        /deep/ .el-form-item__content {
          margin-left: 0 !important;
        }
        .form-btn {
          float: right;
        }
      }
    }
  }
}
</style>

